/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-09-23 10:24:25
 * @filename : static/sea-modules/slider/zepto.sliderDialog.js
 * @version : 
 * @description : 
 */
;(function($){

        var box = null,
            opts = {},
            touch = {move:0,index:0},
            winWidth = window.innerWidth,
            winHeight = window.innerHeight, 
            bool = false,
            defaults = {
                sliderClass : 'li_item',
                mainPhoto : function(){},
                deletePhoto : function(){}
            };

        $(window).bind('orientationchange',function(){
            setTimeout(function() {//android ipad BUG  
                winWidth = window.innerWidth;
                winHeight = window.innerHeight;

                $(box).css({
                    top:$(window).scrollTop()+'px',
                    height:$(window).height()+'px',
                    width:$(window).width()+'px'
                });

                //修正兼容BUG
                $(box).find('ul').css({
                    'height': winHeight+'px',
                    'line-height': winHeight+'px'
                }).find('li').css({
                    'height': winHeight+'px',
                    'width': winWidth+'px'
                }).find('img').css({
                    'max-width' : winWidth+'px',
                    'max-height' : winHeight+'px'
                });
     
                var px = -(touch.index * winWidth);
                touch.move = px;
                methods.setCss(px,0);
             },500);
        });

        /**
         * 字面量方法对象 
         */ 
        var methods = {        

            init : function() {
                touch.sliderNum = $(box).find('.'+opts.sliderClass).length; 
                touch.index = opts.index !== undefined ? opts.index : 0;
                touch.move = -(touch.index * winWidth);
                this.setCss(touch.move,0);

                $(box).css({
                     top:$(window).scrollTop()+'px',
                     height:$(window).height()+'px',
                     width:$(window).width()+'px'
                });

                $(box).find('.value').html(touch.index+1+"/"+touch.sliderNum);
                //修正兼容BUG
                $(box).find('ul').css({
                    'height': winHeight+'px',
                    'line-height': winHeight+'px'
                }).find('li').css({
                    'height': winHeight+'px',
                    'width': winWidth+'px'
                }).find('img').css({
                    'max-width' : winWidth+'px',
                    'max-height' : winHeight+'px'

                });

                this.bindEvt();
            },
 
            bindEvt : function() {
                var tthis = this; 
                
                /*
                $(box).find('.zoom').on('touchend',function(e){
                    e.preventDefault();
                    e.stopPropagation();

                    $(box).find('ul').find('li').eq(touch.index).find('img')
                    .css('-webkit-transform','scale(1.5)').attr('scale','true')
                    .on('touchstart',function(e){
                        //e.preventDefault();
                        //e.stopPropagation();
                    });
 
                });
                */
                   
                $(box).find('.remove').on('touchend',function(){
                     $(box).remove();
                });

                $(box).find('.action').find('span').eq(0).on('touchend',function(e){
                     e.preventDefault();
                     e.stopPropagation();
                     opts.mainPhoto(touch);                    
                });

                $(box).find('.delete').on('touchend',function(e){
                     e.preventDefault();
                     e.stopPropagation();
                     opts.deletePhoto(touch);                    
                });

                $(box).on('touchstart',function(e){
                     touch.timeStart = new Date().getTime();
	                 touch.x1 = e.touches[0].pageX;
	                 touch.y1 = e.touches[0].pageY;
                     touch.x2 = e.touches[0].pageX;
			         touch.y2 = e.touches[0].pageY;
                     bool = true;
                 }).on('touchmove',function(e){
		             if (bool) {
		             	 touch.x2 = e.touches[0].pageX;
			             touch.y2 = e.touches[0].pageY;
                         methods.touchDirection();
			             if (touch.direction == 'x') {
                            e.preventDefault();
                            e.stopPropagation();
			                methods.moveX(touch.x2-touch.x1);
			             } else {
                            e.preventDefault();
                            e.stopPropagation();
                         }
		             }
                 }).on('touchend',function(e){
		             if (bool) {
			             bool = false;
			             touch.timeEnd = new Date().getTime();
			             if (touch.direction == 'x') {
                            e.preventDefault();
                            e.stopPropagation();
			                var timeNum = touch.timeEnd - touch.timeStart;
				            if (touch.x2 - touch.x1 > 30 && timeNum < 500) {
				               methods.swipeRightMove(this);
				            } else if (touch.x2 - touch.x1 < -30 && timeNum < 500){
				               methods.swipeLeftMove(this);
	                        } else {
                               //console.log(touch.x2-touch.x1,timeNum);
				               methods.touchMoveXEnd(touch.x2-touch.x1,this);
                            }
			             }
                      }
                }); 
            },

            swipeRightMove : function(currentSlider) {
                touch.move += winWidth;
                touch.move = methods.touchMoveXCondition(touch.move); 
                //console.log('right',touch.move);
                this.setCss(touch.move,200);
                this.touchCurrentIndex(true,currentSlider);              
                this.touchMoveXAfter();
                $(box).find('.value').html(touch.index+1+"/"+touch.sliderNum); 
            },

            swipeLeftMove : function(currentSlider) {
                touch.move += -winWidth;
                touch.move = methods.touchMoveXCondition(touch.move); 
                //console.log('left',touch.move);
                this.setCss(touch.move,200);
                this.touchCurrentIndex(false,currentSlider); 
                this.touchMoveXAfter();
                $(box).find('.value').html(touch.index+1+"/"+touch.sliderNum);
            },

            touchDirection : function() {
		        touch.direction = 'y';
		        if (Math.abs(touch.x1 - touch.x2) > Math.abs(touch.y1 - touch.y2)) {
		             touch.direction = 'x';
		        } else {
		             touch.direction = 'y';
                }                
            },

            touchScale : function() {
                $(box).find('li').find('img').removeAttr('style');
            },

            touchMoveXAfter : function(currentSlider) {
                $("."+opts.sliderDotsClass).find('b').removeClass('state-active').eq(touch.index).addClass('state-active');
               
            },

            touchCurrentIndex : function(bool,currentSlider) {
                var index = $(box).find('.'+opts.sliderClass).index(currentSlider);
                if (bool) {
                    touch.index -= 1;
                } else {
                    touch.index += 1;
                }

                if(touch.index > touch.sliderNum-1 ) {
                    touch.index = touch.sliderNum-1      
                } else if (touch.index < 0 ) {
                    touch.index = 0;
                }
            },

 
            moveX : function(moveNum) {
                var moveNum = touch.move + moveNum;
                    moveNum = this.touchMoveXCondition(moveNum);
                    this.setCss(moveNum,200);
            },
            
            touchMoveXEnd : function(moveNum,currentSlider) {
               var moveNum = Math.abs(moveNum);
               
               if(moveNum < winWidth/2) {
		          moveNum = 0;
	           } else {
		          moveNum = winWidth;
	           }

              

               if ((touch.x2-touch.x1) < 0 && moveNum !== 0) {
                   moveNum = -moveNum; 
                   this.touchCurrentIndex(false,currentSlider); 
               } else if(moveNum !== 0) {
                   this.touchCurrentIndex(true,currentSlider); 
               }
   
               moveNum = touch.move + moveNum;
               touch.move = this.touchMoveXCondition(moveNum); 
               this.setCss(touch.move,100);
               this.touchMoveXAfter();
               $(box).find('.value').html(touch.index+1+"/"+touch.sliderNum);
            },
           

            touchMoveXCondition : function(moveNum) {
		        if (moveNum > 0 ) {
		            moveNum = 0;
		        } else if (moveNum < -(winWidth * (touch.sliderNum-1))) {
                    moveNum = -(winWidth * (touch.sliderNum-1));
                }
                return moveNum;
            },
           
            setCss : function(moveNum,time) {
                //this.touchScale(); 
                $(box).find('ul').attr('style',' -moz-transform: translate('+moveNum+'px); -moz-transition: '+time+'ms linear; -webkit-transform: translate('+moveNum+'px); -webkit-transition: '+time+'ms linear; transition: '+time+'ms linear; height:'+winHeight+'px;line-height:'+winHeight+'px;');
            } 
        };    

	$.fn.sliderDialog = function(options) {
         box = this;
         opts = $.extend({}, defaults, options || {});	      
         methods.init();
         return $(this);
	}

    if (typeof define === "function" ) {
        define("slider/zepto.sliderDialog.js", [], function () { return $.fn.sliderDialog;} );
    }

})(Zepto);

